// pages/login/login.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ban: true,
        option: false,
        isPassword: true,
        eyeimage: 'eye1.png',
        form: {
            name: "",
            password: ""
        },
        islogin: true
    },
    // 选择是否同意协议
    select() {
        if (this.data.option === false) {
            this.setData({
                ban: false,
                option: true
            });
        } else {
            this.setData({
                ban: true,
                option: false
            });
        }
    },
    // 查看密码可见
    showPassword() {
        if (this.data.isPassword) {
            let isPassword = !this.data.isPassword;
            this.setData({
                isPassword: isPassword,
                eyeimage: "eye.png"
            })
        } else {
            let isPassword = !this.data.isPassword;
            this.setData({
                isPassword: isPassword,
                eyeimage: "eye1.png"
            })
        }
    },
    // 获取表单数据
    getForm(event) {
        const {
            filed
        } = event.currentTarget.dataset;
        const {
            value
        } = event.detail;
        this.setData({
            ['form.' + filed]: value
        });
    },
    // 登录
    login() {
        const {name,password} = this.data.form;
        wx.request({
            url: 'http://localhost:8081/user/login',
            header: {
                'content-type': 'application/json' // 默认值
            },
            method: "POST",
            data:{
                "username":name,
                "password":password
            },
            success: (result) => {
                console.log(result.data);
                const {code,resmsg} = result.data;
                if(code===401){
                    wx.showToast({
                      title: resmsg,
                      duration:1000,
                      icon:"success"
                    })
                    this.setData({
                        "form":{
                            "password":""
                        }
                    });
                }else if(code===400){
                    wx.showToast({
                      title: resmsg,
                      duration:1000,
                      icon:"error"
                    })
                }else{
                    wx.showToast({
                      title: resmsg,
                      duration:1500,
                      icon:"success"
                    });
                    try {
                        wx.setStorageSync('username', name);
                    } catch (e) {console.log(e) }
                    wx.switchTab({
                      url: '/pages/myself/myself',
                    })
                }
            }
        })
    },
    //点击更换登录和注册
    changeOption(e) {
        const {
            filed
        } = e.currentTarget.dataset;
        if (filed === "register") {
            this.setData({
                islogin: false,
                ban: false
            });
        } else {
            this.setData({
                islogin: true
            });
        }
    },
    //注册
    register(e) {
        const {name,password} = this.data.form;
        let username = name.trim();
        if (username != "") {
            wx.request({
                url: 'http://localhost:8081/user/register',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                method: "POST",
                data: {
                    'username': username,
                    "password": password
                },
                success: (result) => {
                    const {code, resmsg} = result.data;
                    if (code === 400) {
                        wx.showToast({
                            title: resmsg,
                            duration: 1500,
                            icon: "error"
                        });
                    }else{
                        wx.showToast({
                            title: resmsg,
                            duration: 1500,
                            icon:"success"
                        });
                        this.setData({
                            islogin: true,
                            ban:true,
                            form:{
                                "name":"",
                                "password":""
                            }
                        });
                    }
                },
            })
        } else {
            this.setData({
                form: {
                    "name": "",
                    "password": ""
                }
            })
            wx.showToast({
                title: "用户账号为空",
                duration: 1000,
                icon: "error"
            });
        }
    }
})